// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package framework

import (
	"cmp"
	_ "embed"
	"os"
	"text/template"

	"github.com/Masterminds/sprig/v3"
)

var (
	ProviderType      = cmp.Or(os.Getenv("PROVIDER_TYPE"), "apisix")
	DisableGatewayAPI = os.Getenv("DISABLE_GATEWAY_API") == "true"
)

const (
	ProviderTypeAPISIX           = "apisix"
	ProviderTypeAPISIXStandalone = "apisix-standalone"

	ConfigProviderTypeYaml = "yaml"
	ConfigProviderTypeEtcd = "etcd"
)

var (
	//go:embed manifests/apisix.yaml
	apisixStandaloneTemplate string
	APISIXStandaloneTpl      *template.Template

	//go:embed manifests/etcd.yaml
	EtcdSpec string

	//go:embed manifests/webhook.yaml
	validatingWebhookTemplate string
	ValidatingWebhookTpl      *template.Template
)

var (
	//go:embed manifests/cert.pem
	TestServerCert string
	//go:embed manifests/key.pem
	TestServerKey string
)

const (
	TestCert = `-----BEGIN CERTIFICATE-----
MIIC1TCCAb2gAwIBAgIJANm/NDY0xwZUMA0GCSqGSIb3DQEBBQUAMBoxGDAWBgNV
BAMTD3d3dy5leGFtcGxlLmNvbTAeFw0yMTEyMjcwNzI0MTNaFw0zMTEyMjUwNzI0
MTNaMBoxGDAWBgNVBAMTD3d3dy5leGFtcGxlLmNvbTCCASIwDQYJKoZIhvcNAQEB
BQADggEPADCCAQoCggEBAPOeWroWLvnbzmRtvYsyRkFVePoMY3LvZqNaxCQpZHD6
ra/fRDTem01YvJjm5qUwrn9YXKBUgcoTfA3vHGYFH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ZqsHREIC33Sily+1NwOm/iuLWTlHv0+4cww
y75Xvl8e2mDPtkoT8wNeHdKXTp4CLzrIvEHzG0qf063ob0hP24ymqy+PgO/WW8Pl
K/iWTpTzc9UKUuhiNPzp2XIMYwKBgQDLYotPuKkQHwGrhI8mZKKTcefzN9Ton2Kw
7qJ3qDPrbDS0yjbU5+TKaNjKlZ+fsPQkHb5aWkGK+hYU6x7X8V/MZrc5MSwtNjPI
QfhTPKsxNSAPqRoTwP+QVXECQhCdjRZCpE6+4aaB/31VbIjTWdhxUyc6zrPLDimX
p0hhEcH2yQKBgAnBna3HfxvSYPXGr2oliajZxvHZ4ze12ct2ok+Q9yro/9sxjk2b
bPrfxMEQAU99RmmNrCIhFG5AwVmSQwRk9JuK0UFm7fLkXcTL6AImwk6G0uQR2Zka
FrB1FqbDK9o81DrzGZgZc/io7JfR6XhjPluWXHY96pbd4jdEIli8D+gzAoGAAn0o
O0eFOh9HA/RRVCTzIF7Ked17C4W3zXZ+Iny6de0TEAtRdHWKBTgXPxNpqqidtDtw
8uYb2zmIP6VI8VeQ1o2DPH3vjnYVWCQGh+48IhQGWmq1WPyJpBiHk4F/do4dcZ9V
H1zfjsOzovH7EqsMzQY5eqzA4oE/3Q09A4MWHpECgYAU3uxxs5g7QwdK01BPXznk
H1y7bdn2LYS5otllOgAfZRX6BUQNFE8RtwA4HNe764SSEkk7EtTckx0d7ar8V1V2
pfxfd8A0pNI54NtnFk8OS0BVkW5SGoBCWRh2nlV1r0B/7np0X61GgE95JDMslRn0
0AOPIR/qFJ1YYT0a7yKyjQ==
-----END PRIVATE KEY-----`
)

func init() {
	tpl, err := template.New("apisix-standalone").Funcs(sprig.TxtFuncMap()).Parse(apisixStandaloneTemplate)
	if err != nil {
		panic(err)
	}
	APISIXStandaloneTpl = tpl

	webhookTpl, err := template.New("validating-webhook").Funcs(sprig.TxtFuncMap()).Parse(validatingWebhookTemplate)
	if err != nil {
		panic(err)
	}
	ValidatingWebhookTpl = webhookTpl
}
